package com.heying.springbootweb.restcontroller;

import com.heying.springbootweb.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Get用于查询资源
 * POST用于添加资源
 * PUT 大量修改
 * Patch 局部修改
 * Delete 删除资源
 * HEAD 请求头
 */

@RestController
@RequestMapping(path="/rest/stu",produces = "application/json")
@Slf4j
@CrossOrigin(origins = "*")
public class StudentRestController {

    private List<Student> students;


    {
        students = new ArrayList<>();
        students.add(new Student(1,"1001","aaa",new Date(),0,1,"music sport",1));
        students.add(new Student(2,"1002","bbb",new Date(),1,2,"music java",1));
        students.add(new Student(3,"1003","ccc",new Date(),1,2,"music sport",1));
        students.add(new Student(4,"1004","ddd",new Date(),0,1,"music sport",1));
        students.add(new Student(5,"1005","eee",new Date(),0,0,"java,sport",1));
    }


    @GetMapping
    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
    public List<Student> queryAll(){
        return students;
    }

    @GetMapping("/{id}")
    public Student findOne(@PathVariable Integer id){
        return students.stream()
                .filter(s->s.getId().equals(id))
                .findFirst().orElseThrow(NoSuchElementException::new);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Student addOne(@RequestBody Student stu) throws IOException {
        stu.setId(students.stream().mapToInt(Student::getId).max().orElse(0)+1);
        students.add(stu);
        return stu;
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public void modOne(@PathVariable Integer id,@RequestBody Student stu){
        students.stream().
                filter(s->s.getId().equals(id)).findFirst().
                ifPresent(s-> BeanUtils.copyProperties(stu,s));
        log.debug("new students is {}",students);
    }


   @PatchMapping("/{id}/{name}")
   @ResponseStatus(HttpStatus.OK)
    public void modOneName(@PathVariable Integer id,@RequestBody String name){
       students.stream().
               filter(s->s.getId().equals(id)).findFirst().
               ifPresent(s-> s.setName(name));
       log.debug("new students is {}",students);
    }

   @DeleteMapping("/{id}")
   @ResponseStatus(HttpStatus.NO_CONTENT)
    public Student delOne(@PathVariable Integer id){
        Student stu = null;
        for(Iterator<Student> it = students.iterator();it.hasNext();){
            Student s = it.next();
            if (s.getId().equals((id))) {
                it.remove();
            }
        }
        if(stu == null){
            throw  new NoSuchElementException();
        }
       log.debug("new students is {}",students);
        return stu;

    }

}
